package com.mapreduce;

import com.bean.YearTemp;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.join.CompositeInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;

public class DataJoinMapReduce extends Configured implements Tool {
    static class DataJoinMapper extends
            Mapper<LongWritable, Text, Text, Text>{
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
            // 00001,Moon Light,1973
            //00002,Scarborough Fair,1970
            //00003,Yesenia,1980
            //00004,Don't Cry For Me Argentina,1975
//            ->
            String[] datas = value.toString().split(",");
            String k2 = datas[0];
            context.write(new Text(k2),value);
//            k2                    v2
//            00001             00001,Moon Light,1973
//            00002             00002,Scarborough Fair,1970
        }
    }
    static class DataJoinReducer extends
            Reducer<Text, Text, NullWritable, Text>{
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, NullWritable, Text>.Context context) throws IOException, InterruptedException {
            for (Text val:values){
                // K3使用null，方便下一步进行处理。
                context.write(NullWritable.get(),val);
            }
        }
    }
    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        // 指定输入输出路径
//        Path input = new Path(
//                "hdfs://192.168.10.11:9000/joindata/one.txt");
//        Path output = new Path(
//                "hdfs://192.168.10.11:9000/joinone/");
        Path input = new Path(
                "hdfs://192.168.10.11:9000/joindata/two.txt");
        Path output = new Path(
                "hdfs://192.168.10.11:9000/jointwo/");

        FileSystem fs = FileSystem.get(
                new URI("hdfs://192.168.10.11:9000")
                ,conf);
        if (fs.exists(output)) fs.delete(output,true);

        //构建Job
        Job job = Job.getInstance(conf);
        job.setJobName("join");
        job.setJarByClass(this.getClass());

        job.setMapperClass(DataJoinMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(DataJoinReducer.class);
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);

        TextInputFormat.addInputPath(job,input);
        TextOutputFormat.setOutputPath(job,output);
        // 设置压缩格式，为了满足join时输出文件不可分割的要求
        TextOutputFormat.setOutputCompressorClass(
                job,GzipCodec.class);
        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new DataJoinMapReduce(),args));
    }
}
